require "Common/define"
require "Logic/Config"
require "Common/functions"

UIDrawChangeOccupation = {};
HelperFunc.SetUIEnv(UIDrawChangeOccupation)
local this = UIDrawChangeOccupation;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.bgClose=nil;
this.btnSure=nil;
this.list={};
this.SelectState=nil;
this.hero1=nil;
this.hero2=nil;

this.tips1=nil;
this.tips2=nil;
this.totalnum=nil;

function UIDrawChangeOccupation.Init(hero1,hero2)
    this.hero1=hero1;
    this.hero2=hero2;
    local name, packageName = getUIName(PanelNames.UIDrawChangeOccupation);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIDrawChangeOccupation.onCreate(obj)
    bgClose=obj:GetChild("bgClose");
    btnSure=obj:GetChild("btnSure");
    tips1=obj:GetChild("tips1");
    tips2=obj:GetChild("tips2");
    SelectState=obj:GetController("SelectState");

    for i = 1, 6 do
        list[i]=obj:GetChild("btn"..i);
    end

    this.tips1.text=getLanguage("Draw_OccupationDraw_Tips1");
    this.tips2.text=getLanguage("Draw_OccupationDraw_Tips2");
    this.HeroType();
    this.SetSele();
    this.addEvent();
end

function UIDrawChangeOccupation.HeroType()
   
  
    for i = 1, 6 do
        local btnCont=list[i]:GetController("SeleState");
        local icon=list[i]:GetChild("icon");
        local title=list[i]:GetChild("title");
        if this.hero1==i or this.hero2==i then         
            btnCont.selectedIndex=1;                     
        end
        icon.url="ui://ICONAtlas/"..EnumConst.HeroTypeIcon[i];
        title.text=EnumConst.HeroTypeName[i];
        list[i].onClick:Clear();
        list[i].onClick:Add(function()  this.SelectHero(i,btnCont) end);
    end
   
end

function UIDrawChangeOccupation.SelectHero(index,cont)
    if this.hero1==index  then
        this.hero1=nil;
        cont.selectedIndex=0;
    elseif this.hero2==index then
        this.hero2=nil;
        cont.selectedIndex=0;
    elseif this.hero1==nil then
        this.hero1=index;
        cont.selectedIndex=1;
    elseif this.hero2==nil then
        this.hero2=index;
        cont.selectedIndex=1;
    elseif this.hero1~=nil and this.hero2~=nil then
        UISysTips.AddMsg(getLanguage("Sys_MsgOccupationDraw_NoMore"));
    else
        -- body
        cont.selectedIndex=0;
    end

    SetSele();

end

function UIDrawChangeOccupation.SetSele()
    if this.hero1==nil and this.hero2==nil then
        SelectState.selectedIndex=0;
        elseif this.hero1==nil or this.hero2==nil then
            SelectState.selectedIndex=1;    
        else
            SelectState.selectedIndex=2;
    end

end

function UIDrawChangeOccupation.addEvent()
    this.btnSure.onClick:Add(this.ChangeHero);
    this.bgClose.onClick:Add(this.CloseUI);
 --   ListenEvent(Config.EventType.Fresh_SelectDrawSave, this.updateAgain);
end

function UIDrawChangeOccupation.removeEvent()
    this.btnSure.onClick:Remove(this.ChangeHero);
    this.bgClose.onClick:Remove(this.CloseUI);
   -- CloseEvent(Config.EventType.Fresh_SelectDrawSave, this.updateAgain);
end

function UIDrawChangeOccupation.ChangeHero()
    if this.hero1==nil or this.hero2==nil then
        UISysTips.AddMsg(getLanguage("Sys_MsgOccupationDraw_NotEnough"));
        return ;
    end
    drawHandler.SendSetVocationDrawHero(this.hero1,this.hero2);
    this.CloseUI();
end

function UIDrawChangeOccupation.CloseUI()
    closeUI(PanelNames.UIDrawChangeOccupation); 
end

function UIDrawChangeOccupation.OnDestroy()
    this.removeEvent();
end

function UIDrawChangeOccupation.OnEnable() 
   
end

function UIDrawChangeOccupation.OnDisable()
   
end


return UIDrawChangeOccupation;